<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   用户订单
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('lottery_order');
$ltable = Table('lottery');

if(IS_POST)
{
	!$_uid && AjaxReturn(-2,'请先登录');
	
	$cid = intval($_POST['cid']);
	!$cid && AjaxReturn(-1,'所选的彩票不存在！');
	
	$cids = [
		//1	=>	'shishicai',	//	三分时时彩
		2	=>	'cqssc',	//	重庆时时彩
		3	=>	'bjpk10',	//	北京PK10
		4	=>	'xy28',	//	幸运28
		//5	=>	'bj28',	//	北京28
		//6	=>	'3fenpk10',	//	三分PK10
		8	=>	'gd11x5',	//	广东11选5
		9	=>	'xjssc',	//	新疆时时彩
		10	=>	'sd11x5',	//	山东11选5
		11	=>	'tjssc',	//	天津时时彩
		12	=>	'sh11x5',	//	上海11选5
		13	=>	'jx11x5',	//	江西11选5
		14	=>	'ahk3',	//	安徽快3
		15	=>	'fc3d',	//	福彩3D
		16	=>	'shssl',	//	上海时时乐
		17	=>	'fcpl3',	//	福彩排列三
	];	
	!array_key_exists($cid,$cids) && AjaxReturn(-1,'所选的彩票不存在！');
	
	$pid = intval($_POST['pid']);
	!$pid && AjaxReturn(-1,'所选的彩票玩法不存在！');
	
	$iid = intval($_POST['iid']);
	!$iid && AjaxReturn(-1,'所选的彩票期数不存在！');
	
	$num = NoBadStr($_POST['num']);
	!$num && AjaxReturn(-1,'请选择需要投票的号码');
	
	$lottery = $db->getrow('SELECT title FROM '.$ltable.' WHERE status = 1 AND id = '.$cid);
	!$lottery && AjaxReturn(-1,'所选的彩票不存在！');
	
	$tmp = $db->getrow('SELECT 1 FROM '.Table('lottery_play').' WHERE id = '.$pid.' AND cid IN (0,'.$cid.') AND status = 1');
	!$tmp && AjaxReturn(-1,'所选的彩票玩法不存在！');
	
	$period = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE id = '.$iid.' AND cid = '.$cid);
	!$period && AjaxReturn(-1,'所选的彩票期数不存在！');
	(($period['stoptime'] - 30) < TIMESTAMP) && AjaxReturn(-1,'该期数投注已经截止！');
	
	//	加载玩法投注
	$action = 'buy';
	include_once(LIB_PATH.'lottery/'.$cids[$cid].'.inc.php');
	
	//	账户余额
	($_Y['user']['money'] < $money) && AjaxReturn(-1,'账户余额不足，请先充值');
	
	//	入库
	$ins = [];
	$ins['uid']		=	$_uid;
	$ins['cid']		=	$cid;
	$ins['iid']		=	$iid;
	$ins['pid']		=	$pid;	//	玩法
	$ins['period']	=	$period['period'];	//	期号
	$ins['money']	=	$money;	//	投注金额
	$ins['rednum']	=	$rednum;	//	红球
	$ins['bluenum']	=	$bluenum;	//	篮球，如果有的话
	$ins['multiple']=	$multiple;	//	倍数
	$ins['status']	=	1;
	$ins['dateline']=	TIMESTAMP;
	$id = $db->add('lottery_order',$ins);
	!$id && AjaxReturn(-1,'投注失败，请重新投注');
	
	//	资金日志
	$ins = [];
	$ins['uid']		=	$_uid;
	$ins['tid']		=	2;
	$ins['fee']		=	-$money;
	$ins['money']	=	$_Y['user']['money'] - $money;
	$ins['notes']	=	'购买“'.$lottery['title'].'”，订单：'.$id;
	$ins['dateline']=	TIMESTAMP;
	$db->add('user_money_log',$ins);
	
	//	扣款
	$db->exec('UPDATE '.Table('user').' SET money = money - '.$money.' WHERE id = '.$_uid);
	
	AjaxReturn($id,'投注成功！');
}

if(!$_uid)
{
	header('Location: '.MU('login'));
	exit;
}

if($a == 'view')	//	详情
{
	$id = intval($_GET['id']);
	!$id && ShowMsg('查看的订单不存在！',MU());
	
	$data = $db->getrow('SELECT t.*,l.title,l.thumb FROM '.$table.' t LEFT JOIN '.$ltable.' l ON t.cid = l.id WHERE t.id = '.$id.' AND t.uid = '.$_uid);
	!$data && ShowMsg('查看的订单不存在！',MU());
	
	//	开奖信息
	$period = $db->getrow('SELECT * FROM '.Table('lottery_period').' WHERE id = '.$data['iid']);
	
	//	玩法
	$play = ['name'=>'直选单式'];
	$data['pid'] && $play = $db->getrow('SELECT name FROM '.Table('lottery_play').' WHERE id = '.$data['pid']);
	
	$tpl->display('wap/order_view');
	exit;
}

$data  = [];
$where = ' FROM '.$table.' t LEFT JOIN '.$ltable.' l ON t.cid = l.id WHERE t.uid = '.$_uid;

$cid = intval($_GET['cid']);
$cid && $where .= ' AND t.cid = '.$cid;

$a == 'prize' && $where .= ' AND t.prize > 0';

$title = NoBadStr($_GET['title']);
$title && $where .= ' AND l.title LIKE \'%'.$title.'%\'';

$page = max(1,intval($_GET['page']));
$size = 20;

$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
if($tmp['p'])
{
	$limit = ' LIMIT '.($page - 1) * $size.','.$size;

	$url = MU($c.'/'.$a,['cid'=>$cid,'title'=>$title]);
	$orderby = ' ORDER BY t.id DESC ';
	
	$data = $db->getall('SELECT t.*,l.title '.$where.$orderby.$limit);
	
	//$multi = Multi($url,$page,$tmp['p'],$size);
}

$tpl->display('wap/order'.($a == 'prize' ? '_prize' : ''));